package com.shop.shoporder.wapController;

import com.shop.shoporder.common.*;


import com.shop.shoporder.entity.*;
import com.shop.shoporder.service.StoreGoodsService;
import com.shop.shoporder.service.StoreService;
import com.shop.shoporder.service.WapGoodsDetailsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@CrossOrigin
@Controller
@RequestMapping("/wap")
public class WapGoodsDetailController {
    @Autowired
    WapGoodsDetailsService wapGoodsDetailsService;
    @Autowired
    StoreService storeService;

    @Autowired
    StoreGoodsService storeGoodsService;


    /**
     * 获取商品详情
     * @param map
     * @param request
     * @return
     */
    @PassToken
    @ResponseBody
    @RequestMapping("/getGoodsDetailsById")
    public Response getGoodsDetailsById(@RequestParam Map<String, String> map, HttpServletRequest request) {
        if (map != null) {
            try {
                WapGoodsSpec wapGoodsSpec = null;
                int userNoId = 0;
                int rankId = 0;
                if (map.get("userNoId") != null) {
                    userNoId = Integer.parseInt(map.get("userNoId"));
                }
                if (map.get("rankId") != null) {
                    rankId = Integer.parseInt(map.get("rankId"));
                }

                wapGoodsSpec = wapGoodsDetailsService.selectGoodsDetailsByGoodId(map);
                if (wapGoodsSpec != null) {
                    List<String> picList = new ArrayList<>();
                    GoodsDetails goodsDetails = new GoodsDetails();
                    //图组
                    String[] picArr = wapGoodsSpec.getPiclist().split(",");
                    for (int i = 0; i < picArr.length; i++) {
                        picList.add(picArr[i]);
                    }
                    goodsDetails.setPiclist(picList);

                    //详情图
                    List<String> picDetailsList = new ArrayList<>();

                    String[] picDetatilsArr = wapGoodsSpec.getPicdetails().split(",");

                    for (int i = 0; i < picDetatilsArr.length; i++) {
                        picDetailsList.add(picDetatilsArr[i]);
                    }
                    goodsDetails.setZoneId(wapGoodsSpec.getZoneId());
                    goodsDetails.setPicdetails(picDetailsList);
                    goodsDetails.setName(wapGoodsSpec.getName());
                    goodsDetails.setTitle(wapGoodsSpec.getTitle());
                    goodsDetails.setId(wapGoodsSpec.getId());
                    goodsDetails.setNameDesc(wapGoodsSpec.getNameDesc());
                    for (int i = 0; i < wapGoodsSpec.getSpace().size(); i++) {
                        goodsDetails.setConsumption(wapGoodsSpec.getSpace().get(i).getConsumption());
                        goodsDetails.setPrice(wapGoodsSpec.getSpace().get(i).getPrice());
                        goodsDetails.setCoupon(wapGoodsSpec.getSpace().get(i).getCoupon());
                        goodsDetails.setShare(wapGoodsSpec.getSpace().get(i).getShare());
                        goodsDetails.setSpecId(wapGoodsSpec.getSpace().get(i).getId());
                        goodsDetails.setVouchers(wapGoodsSpec.getSpace().get(i).getVouchers());
                        goodsDetails.setPackSpec(wapGoodsSpec.getSpace().get(i).getPackSpec());

                        switch (rankId) {
                            case 3:
                                goodsDetails.setOwner(wapGoodsSpec.getSpace().get(i).getOwner1());
                                break;
                            case 4:
                                goodsDetails.setOwner(wapGoodsSpec.getSpace().get(i).getOwner2());
                                break;
                            case 5:
                                goodsDetails.setOwner(wapGoodsSpec.getSpace().get(i).getOwner3());
                                break;
                            case 6:
                                goodsDetails.setOwner(wapGoodsSpec.getSpace().get(i).getOwner4());
                                break;
                            default:
                                goodsDetails.setOwner(0.00);
                                break;

                        }
                    }

                    return new Response(MessageConstants.MSG_SUCCESS, goodsDetails, 0, "", request);
                } else {

                    return new Response(MessageConstants.MSG_REQUEST_STATE, null, 0, "", request);

                }

            } catch (Exception e) {
                e.printStackTrace();
                return new Response(MessageConstants.MSG_SYS_ERROR, null, 0, "", request);
            }
        } else {
            return new Response(MessageConstants.MSG_ILLEGAL_ARGUMENT, null, 0, "", request);
        }

    }


    /**
     * 根据商品Id查询规格
     */
    @PassToken
    @ResponseBody
    @RequestMapping("/getDetailsSpecListByGoodsId")
    public Response getDetailsSpecListByGoodsId(@RequestParam Map<String, String> map, HttpServletRequest request) {
        if (map != null) {
            List<GoodsDetailsSpce> goodsDetailsSpceList = wapGoodsDetailsService.selectDetailsSpecListByGoodsId(map);
            if (goodsDetailsSpceList != null && goodsDetailsSpceList.size() != 0) {
                return new Response(MessageConstants.MSG_SUCCESS, goodsDetailsSpceList, 0, "", request);
            } else {
                return new Response(MessageConstants.MSG_REQUEST_STATE, null, 0, "", request);
            }
        } else {
            return new Response(MessageConstants.MSG_ILLEGAL_ARGUMENT, null, 0, "", request);
        }
    }


    /**
     * 加入店铺
     * -1：没有店铺，0加入失败，1加入成功，2已加入过
     * @param request
     * @return
     */
    @UserLoginToken
    @ResponseBody
    @RequestMapping("/addStore")
    public Response addStore(HttpServletRequest request) {
        String userNoIds = request.getParameter("userNoId");
        String pIds = request.getParameter("pId");
        String specIds = request.getParameter("sId");
        int userNoId = 0;
        int storeId = 0;
        Store store = null;
        int pId = 0;
        int specId = 0;
        if (StringUtil.isNumeric(userNoIds) || StringUtil.isNumeric(pIds) || StringUtil.isNumeric(specIds)) {
            userNoId = Integer.parseInt(userNoIds);
            pId = Integer.parseInt(pIds);
            specId = Integer.parseInt(specIds);

            //查询店铺ID
            store = storeService.selectStoreInfo(userNoId);
            if (store == null) {
                return new Response(MessageConstants.MSG_SUCCESS, -1, 0, "", request);
            }

            storeId = store.getStoreId();
            List<StoreGoods> storeGoodsList = storeGoodsService.selectStoreGoodList(storeId);
            if (storeGoodsList != null && storeGoodsList.size() != 0) {
                for (int i = 0; i < storeGoodsList.size(); i++) {
                    if (pId == storeGoodsList.get(i).getGoodsId() && specId == storeGoodsList.get(i).getSpecId()) {
                        return new Response(MessageConstants.MSG_SUCCESS, 2, 0, "已经加入到店铺！", request);
                    }

                }
            }

        } else {
            return new Response(MessageConstants.MSG_ILLEGAL_ARGUMENT, null, 0, "", request);
        }

        //加入店铺
        StoreGoods storeGoods = new StoreGoods();
        storeGoods.setGoodsId(pId);
        storeGoods.setSpecId(specId);
        storeGoods.setSort(100);
        storeGoods.setStoreId(storeId);
        storeGoods.setUpState("1");
        int row = storeGoodsService.insertSelective(storeGoods);

        if (row > 0) {
            return new Response(MessageConstants.MSG_SUCCESS, 1, 0, "", request);
        } else {
            return new Response(MessageConstants.MSG_SUCCESS, 0, 0, "", request);
        }

    }


}
